package com.voicechat.firstmet.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.luck.picture.lib.entity.LocalMedia;
import com.voicechat.firstmet.R;
import com.voicechat.firstmet.base.BaseAdapter;
import com.voicechat.firstmet.view.CircleImageView;

import java.util.List;

/**
 * 模块简介：发帖页面图片集合adapter
 */
public class CirclePictureAdapter extends BaseAdapter<LocalMedia> {
    private int flag;
    private String TAG = "CirclePictureAdapter";
    private List<LocalMedia> tlist;

    /**
     * @param context activity_imessage
     * @param tlist   集合
     * @param mLayout 主布局
     */
    public CirclePictureAdapter(Context context, List<LocalMedia> tlist, int mLayout, int flag) {
        super(context, tlist, mLayout);
        this.flag = flag;
        this.tlist = tlist;
    }
    public void setList(List<LocalMedia> list){
        tlist.clear();
        tlist.addAll(list);
        Log.i(TAG, "setList: ----"+tlist.size());
        notifyDataSetChanged();
    }

    public List<LocalMedia> getTlist() {
        return tlist;
    }

    @Override
    protected RecyclerView.ViewHolder getHeaderViewHolder(View view) {
        return null;
    }

    @Override
    protected RecyclerView.ViewHolder getItemViewHolder(View view) {
        return new ItemViewHolder(view);
    }

    @Override
    protected RecyclerView.ViewHolder getFooterViewHolder(View view) {
        return new FooterViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        if (viewHolder instanceof ItemViewHolder) {
            operationItemView((ItemViewHolder) viewHolder, position);
        } else if (viewHolder instanceof FooterViewHolder) {
            operationFooterView((FooterViewHolder) viewHolder, position);
        }
    }

    protected void operationItemView(ItemViewHolder holder, final int position) {

        if(tlist.size()>0){
        LocalMedia info = tlist.get(position);
           // Log.i(TAG, "operationItemView: --isCompressed--"+info.isCompressed());
           // Log.i(TAG, "operationItemView: --getCompressPath--"+info.getCompressPath());
        Glide.with(context).load(info.getCompressPath()).into(holder.picture);
        holder.relativeLayoutPicture.setVisibility(View.VISIBLE);
        holder.delet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onAddClick.setRemoveItemClick(position);
            }
        });
        }
    }

    protected void operationFooterView(FooterViewHolder holder, final int position) {
        int count = tlist == null ? 0 : tlist.size();
        if(flag == 1){
            holder.relativeLayoutAdd.setVisibility(View.VISIBLE);
            holder.relativeLayoutAdd.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onAddClick.setAddItemClick(position);
                }
            });
        }else {
            if (count >= 1) {
                holder.relativeLayoutAdd.setVisibility(View.VISIBLE);
                holder.relativeLayoutAdd.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onAddClick.setAddItemClick(position);
                    }
                });
            }
        }
    }

    /**
     * 刷新当前Adapter
     */
    public void refreshCurrentAdapter() {
        Log.i(TAG, "refreshCurrentAdapter: ----"+tlist.size());
        int count = tlist == null ? 0 : tlist.size();
        if (count >= 9) {
            cleaMoreView();
            onAddClick.setSendImgBg(true);
        } else {
            if (getIdentification() == NOLAYOUT) {
                setIdentification(TAILLAYOUT);
            }
        }
        notifyDataSetChanged();
    }

    private OnAddClick onAddClick;

    public void setAddClick(OnAddClick onAddClick) {
        this.onAddClick = onAddClick;
    }

    private class ItemViewHolder extends BaseViewHolder {
        private CircleImageView picture;
        private ImageView delet;
        private RelativeLayout relativeLayoutAdd;
        private RelativeLayout relativeLayoutPicture;

        public ItemViewHolder(View itemView) {
            super(itemView);
           // if (flag == 0) {
                picture = (CircleImageView) itemView.findViewById(R.id.image_picture_circle);
                delet = (ImageView) itemView.findViewById(R.id.image_picture_dele);
            relativeLayoutAdd = itemView.findViewById(R.id.rela_add);
            relativeLayoutPicture = itemView.findViewById(R.id.rela_picture);
           /* }else if(flag==1){
                ///picture = (ImageView) itemView.findViewById(R.id.image_picture_evidence);
                delet = (ImageView) itemView.findViewById(R.id.image_picture_dele);
            }*/

        }
    }

    private class FooterViewHolder extends BaseViewHolder {
        private RelativeLayout relativeLayoutAdd;
        private RelativeLayout relativeLayoutPicture;


        public FooterViewHolder(View itemView) {
            super(itemView);
            relativeLayoutAdd = itemView.findViewById(R.id.rela_add);
            relativeLayoutPicture = itemView.findViewById(R.id.rela_picture);
        }
    }


    public interface OnAddClick {
        void setAddItemClick(int position);

        void setRemoveItemClick(int position);

        void setSendImgBg(boolean flag);
    }
}
